/*
 * Main.java
 *
 * Created on March 16, 2007, 3:29 PM
 *
 */
/*
 *
 * @author Joseph F. Pleso
 */


package edu.psu.bd.math.vertexreplacer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Paint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.Vertex;
import edu.uci.ics.jung.graph.decorators.EdgeShape;
import edu.uci.ics.jung.graph.decorators.Indexer;
import edu.uci.ics.jung.graph.decorators.VertexPaintFunction;
import edu.uci.ics.jung.graph.impl.UndirectedSparseGraph;
import edu.uci.ics.jung.utils.UserData;
import edu.uci.ics.jung.visualization.AbstractLayout;
import edu.uci.ics.jung.visualization.DefaultSettableVertexLocationFunction;
import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
import edu.uci.ics.jung.visualization.PluggableRenderer;
import edu.uci.ics.jung.visualization.ShapePickSupport;
import edu.uci.ics.jung.visualization.StaticLayout;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.EditingModalGraphMouse;
import edu.uci.ics.jung.visualization.control.EditingPopupGraphMousePlugin;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse;

public class GraphEditor {

    private static UndirectedSparseGraph graph;
    private static java.util.List<UndirectedSparseGraph> graphs
            = new ArrayList<UndirectedSparseGraph>();
    private static AbstractLayout layout;
    private static JButton plus;
    private static GraphZoomScrollPane panel;
    private static JPanel controls;
    private static JFrame frame;
    private static boolean isReplacement = false;
    private static boolean stage = true;
    private static VisualizationViewer vv;
    private static DefaultSettableVertexLocationFunction vertexLocations;
    
    
    
    private static void plus(){
        if (stage)
        {
            stage = false;
            isReplacement = true;
            frame.remove(panel);
            frame.remove(controls);
            if (graphs.isEmpty())
                setLocations();
            graphs.add(graph);
            GraphEditor2();
        }
        else
        {
            stage = true;
            markBoundary();
            changeText();
        }
        
    }
    
    private static void setLocations (){
        Indexer indexer = Indexer.getAndUpdateIndexer(graph);
        
        
        for (int i = 0; i < graph.numVertices(); i++)
        {
            Vertex v = (Vertex) indexer.getVertex(i);
            v.setUserDatum("x",layout.getX(v),UserData.SHARED);
            v.setUserDatum("y",layout.getY(v),UserData.SHARED);
        }
    }
    
    private static void markBoundary(){
        
        Indexer i = Indexer.getAndUpdateIndexer( (Graph) graph);
        
        for (int j=0;j<graph.numVertices();j++) {
            i.getVertex(j).setUserDatum("isBoundary", "true", UserData.SHARED);
        }
        
    }
    
    

    
    
    private static void reset(){
        plus();
        frame.remove(panel);
        frame.remove(controls);
        
        
        new ShowLayout(new VertexReplacement (graphs),frame);
    }
    
    private static void changeText(){
        plus.setText("Press When Finished with Replacement Graph");
    }
    
    public UndirectedSparseGraph getGraph(){
        return graph;
    }
    
    
    public static void start(JFrame jf) {
        frame = jf;
        
        GraphEditor2();
    }
    
    
    private static class VertexColor implements VertexPaintFunction   {
    
        public Paint getDrawPaint(Vertex v) {
            return Color.BLACK;
        }
        
        public Paint getFillPaint(Vertex v) {
            Color c = Color.BLACK;
            if (v.containsUserDatumKey("isBoundary")) {
                if (((String)v.getUserDatum("isBoundary"))=="true")
                    c = Color.RED;
            }
            return c;
            
        }
    }
    
    
    
    private static void GraphEditor2() {
        
        vertexLocations = new DefaultSettableVertexLocationFunction();
        
        graph = new UndirectedSparseGraph();
        
        
        
        PluggableRenderer pr = new PluggableRenderer();
        layout = new StaticLayout(graph);
        layout.initialize(Main.DIMENSION, vertexLocations);
        
        vv =  new VisualizationViewer(layout, pr);
        vv.setBackground(Color.white);
        vv.setPickSupport(new ShapePickSupport());
        pr.setEdgeShapeFunction(new EdgeShape.Line());
        
        
        
        
        
        pr.setVertexPaintFunction(new VertexColor());
        
        
        
        
        
        
        Container content = frame.getContentPane();
        panel = new GraphZoomScrollPane(vv);
        content.add(panel);
        EditingModalGraphMouse graphMouse = new EditingModalGraphMouse();
        
 
        graphMouse.setVertexLocations(vertexLocations);
        vv.setGraphMouse(graphMouse);
        graphMouse.add(new EditingPopupGraphMousePlugin(vertexLocations));
        graphMouse.setMode(ModalGraphMouse.Mode.EDITING);
        
        //final ScalingControl scaler = new CrossoverScalingControl();
        
        
        //JRadioButton
        
        
        
        
        plus = new JButton("Press When Finished with Initial Graph");
        plus.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
                plus();
            }
        });
        if (isReplacement) {
            plus.setText("Press When Finished Placing Boundary Vertices");
        }
        
        JButton help = new JButton("Done");
        help.addActionListener(new ActionListener() {
            
            public void actionPerformed(ActionEvent e) {
                reset();
            }});
            
            controls = new JPanel();
            controls.add(plus);
            JComboBox modeBox = graphMouse.getModeComboBox();
            controls.add(modeBox);
            controls.add(help);
            content.add(controls, BorderLayout.SOUTH);
            frame.pack();
            frame.setVisible(true);
    }
}
